<?php

namespace app\admin\model;

use think\Model;
use think\Cache;

class School extends Model
{
    // 表名
    protected $name = 'school';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'category_text',
        'type_text',
        'policylist_text',
        'is_schoolsdata_text',
        'volume_text',
        'teaching_order_text',
        'province_text',
        'city_text',
        'area_text',
    ];
    

    
    public function getCategoryList()
    {
        return ['A' => __('A'),'B' => __('B'),'C' => __('C'),'D' => __('D'),'E' => __('E')];
    }     

    public function getTypeList()
    {
        return ['1' => __('小学'),'2' => __('初中'),'3' => __('高中'),'4' => __('其他')];
    }     

    public function getPolicylistList()
    {
        return ['1' => __('Policylist 1'),'2' => __('Policylist 2'),'3' => __('Policylist 3'),'4' => __('Policylist 4')];
    }     

    public function getIsSchoolsdataList()
    {
        return ['1' => __('Is_schoolsdata 1'),'2' => __('Is_schoolsdata 2')];
    }     

    public function getVolumeList()
    {
        return ['1' => __('Volume 1'),'2' => __('Volume 2'),'3' => __('Volume 3'),'4' => __('Volume 4'),'5' => __('Volume 5')];
    }     

    public function getTeachingOrderList()
    {
        return ['1' => __('Teaching_order 1'),'2' => __('Teaching_order 2'),'3' => __('Teaching_order 3')];
    }

    /**
     * 获取省的名称
     * @return array
     */
    public function getProvinceOrderList()
    {
        if(!$list = Cache::get('arealist'))
        {
            $area = Area::field('id,name')->select();
            $area = collection($area)->toArray();
            $list = array_column($area,'name','id');
            Cache::set('areaList',$list,0);//设置0永久缓存不过期
        }
        return $list;
    }

    public function getCityOrderList()
    {
        if(!$list = Cache::get('arealist'))
        {
            $area = Area::field('id,name')->select();
            $area = collection($area)->toArray();
            $list = array_column($area,'name','id');
            Cache::set('areaList',$list,0);//设置0永久缓存不过期
        }
        return $list;
    }

    public function getAreaOrderList()
    {
        if(!$list = Cache::get('arealist'))
        {
            $area = Area::field('id,name')->select();
            $area = collection($area)->toArray();
            $list = array_column($area,'name','id');
            Cache::set('areaList',$list,0);//设置0永久缓存不过期
        }
        return $list;
    }

    public function getProvinceTextAttr($value, $data)
    {
        $value = $value ? $value : $data['province'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCityTextAttr($value, $data)
    {
        $value = $value ? $value : $data['city'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAreaTextAttr($value, $data)
    {
        $value = $value ? $value : $data['area'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCategoryTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['category'];
        $list = $this->getCategoryList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPolicylistTextAttr($value, $data)
    {
        $value = $value ? $value : $data['policylist'];
        $valueArr = explode(',', $value);
        $list = $this->getPolicylistList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getIsSchoolsdataTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['is_schoolsdata'];
        $list = $this->getIsSchoolsdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVolumeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['volume'];
        $list = $this->getVolumeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTeachingOrderTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['teaching_order'];
        $list = $this->getTeachingOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPolicylistAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function apiSchoolList($page)
    {
        return School::page("{$page},10")->select();
    }

}
